<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="微服务架构之SpringCloud学习前提 JavaSE –(Java基础) Mysql Servlet Http Mybatis Spring SpringMVC SpringBoot Dubbo Zookeeper 分布式基础 Maven Git Ajax Json  AND　SO　ＯＮ  有了以上基础，那么可以学习SpringCloud了 微服务架构四个核心问题 服务众多 客户端该如何访问">
<meta name="keywords" content="Java,SpringBoot,SpringCloud">
<meta property="og:type" content="article">
<meta property="og:title" content="微服务架构之SpringCloud">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;11&#x2F;26&#x2F;%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud&#x2F;index.html">
<meta property="og:site_name" content="ECIN&#39;S BLOG">
<meta property="og:description" content="微服务架构之SpringCloud学习前提 JavaSE –(Java基础) Mysql Servlet Http Mybatis Spring SpringMVC SpringBoot Dubbo Zookeeper 分布式基础 Maven Git Ajax Json  AND　SO　ＯＮ  有了以上基础，那么可以学习SpringCloud了 微服务架构四个核心问题 服务众多 客户端该如何访问">
<meta property="og:locale" content="en">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574691503030.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574694996433.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574696001295.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574697283154.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574697315323.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574698895388.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574699028571.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574700133565.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574700380924.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574700389886.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574700400872.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574732949690.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574768395963.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574773523980.jpg">
<meta property="og:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574773579130.jpg">
<meta property="og:updated_time" content="2019-11-27T00:21:51.841Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http:&#x2F;&#x2F;148.70.50.70&#x2F;source&#x2F;1574691503030.jpg">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>微服务架构之SpringCloud</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2020/01/18/Flutter%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/11/19/Netty%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E7%9E%8E%E6%90%9E/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&text=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&is_video=false&description=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=微服务架构之SpringCloud&body=Check out this article: http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/" target="_blank" rel="noopener"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&name=微服务架构之SpringCloud&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&t=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#微服务架构之SpringCloud"><span class="toc-number">1.</span> <span class="toc-text">微服务架构之SpringCloud</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#学习前提"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">学习前提</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#微服务架构四个核心问题"><span class="toc-number">1.0.0.2.</span> <span class="toc-text">微服务架构四个核心问题</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#解决方案"><span class="toc-number">1.0.0.2.0.1.</span> <span class="toc-text">解决方案</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#微服务与微服务架构"><span class="toc-number">1.0.0.3.</span> <span class="toc-text">微服务与微服务架构</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#微服务"><span class="toc-number">1.0.0.3.1.</span> <span class="toc-text">微服务</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#微服务架构"><span class="toc-number">1.0.0.3.2.</span> <span class="toc-text">微服务架构</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SpringCloud-Begin"><span class="toc-number">1.0.1.</span> <span class="toc-text">SpringCloud Begin</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#基础项目搭建"><span class="toc-number">1.0.1.1.</span> <span class="toc-text">基础项目搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#父项目"><span class="toc-number">1.0.1.1.1.</span> <span class="toc-text">父项目</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#写一个api提供者"><span class="toc-number">1.0.1.1.2.</span> <span class="toc-text">写一个api提供者</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建一个服务提供者，添加依赖"><span class="toc-number">1.0.1.1.3.</span> <span class="toc-text">创建一个服务提供者，添加依赖</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建消费者consumer"><span class="toc-number">1.0.1.1.4.</span> <span class="toc-text">创建消费者consumer</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#简单配置pom"><span class="toc-number">1.0.1.1.4.1.</span> <span class="toc-text">简单配置pom</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加配置，创建一个REST配置类RestConfig"><span class="toc-number">1.0.1.1.4.2.</span> <span class="toc-text">添加配置，创建一个REST配置类RestConfig</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#创建一个UserConsumerController类"><span class="toc-number">1.0.1.1.4.3.</span> <span class="toc-text">创建一个UserConsumerController类</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#application-yml配置端口为8080，启动provider提供者，然后访问"><span class="toc-number">1.0.1.1.4.4.</span> <span class="toc-text">application.yml配置端口为8080，启动provider提供者，然后访问</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#添加Eureka服务注册中心"><span class="toc-number">1.0.1.2.</span> <span class="toc-text">添加Eureka服务注册中心</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#修改一下host文件模拟不同地址"><span class="toc-number">1.0.1.2.1.</span> <span class="toc-text">修改一下host文件模拟不同地址</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建一个Eureka服务端"><span class="toc-number">1.0.1.2.2.</span> <span class="toc-text">创建一个Eureka服务端</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖"><span class="toc-number">1.0.1.2.2.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置yml"><span class="toc-number">1.0.1.2.2.2.</span> <span class="toc-text">配置yml</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端"><span class="toc-number">1.0.1.2.2.3.</span> <span class="toc-text">再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#也可以在Controller中写一下获取微服务实例的代码"><span class="toc-number">1.0.1.2.2.4.</span> <span class="toc-text">也可以在Controller中写一下获取微服务实例的代码</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#最重要的就是配置yml，在之前的yml中添加，最重要的就是spring-application-name（这个会是一个服务标识）和eureka-client-service-url（这是注册中心的地址）"><span class="toc-number">1.0.1.2.2.5.</span> <span class="toc-text">最重要的就是配置yml，在之前的yml中添加，最重要的就是spring.application.name（这个会是一个服务标识）和eureka.client.service-url（这是注册中心的地址）</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务"><span class="toc-number">1.0.1.2.2.6.</span> <span class="toc-text">首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#配置Eureka集群环境"><span class="toc-number">1.0.1.3.</span> <span class="toc-text">配置Eureka集群环境</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003"><span class="toc-number">1.0.1.3.1.</span> <span class="toc-text">首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-1"><span class="toc-number">1.0.1.3.1.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类配置注解"><span class="toc-number">1.0.1.3.1.2.</span> <span class="toc-text">主启动类配置注解</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置yml-1"><span class="toc-number">1.0.1.3.1.3.</span> <span class="toc-text">配置yml</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7002的配置"><span class="toc-number">1.0.1.3.1.4.</span> <span class="toc-text">7002的配置</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7003"><span class="toc-number">1.0.1.3.1.5.</span> <span class="toc-text">7003</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#修改provider，把服务提供到的注册中心由一个增加到三个"><span class="toc-number">1.0.1.3.2.</span> <span class="toc-text">修改provider，把服务提供到的注册中心由一个增加到三个</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在yml中"><span class="toc-number">1.0.1.3.2.1.</span> <span class="toc-text">在yml中</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Eureka熔断机制"><span class="toc-number">1.0.1.3.3.</span> <span class="toc-text">Eureka熔断机制</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#自我保护机制"><span class="toc-number">1.0.1.3.3.1.</span> <span class="toc-text">自我保护机制</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#CAP原则"><span class="toc-number">1.0.1.3.3.2.</span> <span class="toc-text">CAP原则</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Ribben负载均衡"><span class="toc-number">1.0.1.4.</span> <span class="toc-text">Ribben负载均衡</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#修改consumer"><span class="toc-number">1.0.1.4.1.</span> <span class="toc-text">修改consumer</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在上面基础的配置下添加"><span class="toc-number">1.0.1.4.1.1.</span> <span class="toc-text">在上面基础的配置下添加</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#将原来的UserConsumerController中的URL换成"><span class="toc-number">1.0.1.4.1.2.</span> <span class="toc-text">将原来的UserConsumerController中的URL换成</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置注册中心地址（从哪里拿）"><span class="toc-number">1.0.1.4.1.3.</span> <span class="toc-text">配置注册中心地址（从哪里拿）</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在最外层的同级包下创建一个规则配置rule，具体位置"><span class="toc-number">1.0.1.4.1.4.</span> <span class="toc-text">在最外层的同级包下创建一个规则配置rule，具体位置</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#开启负载均衡，修改RestConfig"><span class="toc-number">1.0.1.4.1.5.</span> <span class="toc-text">开启负载均衡，修改RestConfig</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加负载均衡随机轮询规则"><span class="toc-number">1.0.1.4.1.6.</span> <span class="toc-text">添加负载均衡随机轮询规则</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#最后在启动类中添加注解"><span class="toc-number">1.0.1.4.1.7.</span> <span class="toc-text">最后在启动类中添加注解</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#consumer修改好了后，开始修改provider"><span class="toc-number">1.0.1.4.2.</span> <span class="toc-text">consumer修改好了后，开始修改provider</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加两个数据库"><span class="toc-number">1.0.1.4.2.1.</span> <span class="toc-text">添加两个数据库</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#都是同样的内容"><span class="toc-number">1.0.1.4.2.2.</span> <span class="toc-text">都是同样的内容</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加两个服务提供者provider-8002-provider-8003"><span class="toc-number">1.0.1.4.2.3.</span> <span class="toc-text">添加两个服务提供者provider-8002 provider-8003</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-2"><span class="toc-number">1.0.1.4.2.4.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#修改后的三个provider后的application-yml为"><span class="toc-number">1.0.1.4.2.5.</span> <span class="toc-text">修改后的三个provider后的application.yml为</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#测试"><span class="toc-number">1.0.1.4.3.</span> <span class="toc-text">测试</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#启动一个Eureka，三个provider以及一个consumer"><span class="toc-number">1.0.1.4.3.1.</span> <span class="toc-text">启动一个Eureka，三个provider以及一个consumer</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#feign接口方式调用服务"><span class="toc-number">1.0.1.5.</span> <span class="toc-text">feign接口方式调用服务</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#springcloud-api中"><span class="toc-number">1.0.1.5.1.</span> <span class="toc-text">springcloud-api中</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-3"><span class="toc-number">1.0.1.5.1.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加service-UserService接口"><span class="toc-number">1.0.1.5.1.2.</span> <span class="toc-text">添加service.UserService接口</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建一个feign测试项目springcloud-feign"><span class="toc-number">1.0.1.5.2.</span> <span class="toc-text">创建一个feign测试项目springcloud-feign</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-4"><span class="toc-number">1.0.1.5.2.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置application-yml"><span class="toc-number">1.0.1.5.2.2.</span> <span class="toc-text">配置application.yml</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类，添加扫描包注解"><span class="toc-number">1.0.1.5.2.3.</span> <span class="toc-text">主启动类，添加扫描包注解</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#创建UserConsumerController类"><span class="toc-number">1.0.1.5.2.4.</span> <span class="toc-text">创建UserConsumerController类</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置一下RestConfig"><span class="toc-number">1.0.1.5.2.5.</span> <span class="toc-text">配置一下RestConfig</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#其实和上面的consumer是一样的，只不过有些许地方不太一样"><span class="toc-number">1.0.1.5.2.6.</span> <span class="toc-text">其实和上面的consumer是一样的，只不过有些许地方不太一样</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#服务熔断"><span class="toc-number">1.0.1.6.</span> <span class="toc-text">服务熔断</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#分布式系统面临的问题"><span class="toc-number">1.0.1.6.0.1.</span> <span class="toc-text">分布式系统面临的问题</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#服务雪崩"><span class="toc-number">1.0.1.6.0.2.</span> <span class="toc-text">服务雪崩</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Hystrix"><span class="toc-number">1.0.1.6.0.3.</span> <span class="toc-text">Hystrix</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Hystrix用处"><span class="toc-number">1.0.1.6.0.4.</span> <span class="toc-text">Hystrix用处</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#服务熔断-1"><span class="toc-number">1.0.1.6.0.5.</span> <span class="toc-text">服务熔断</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#添加一个子项目Hystrix实现服务熔断处理"><span class="toc-number">1.0.1.6.1.</span> <span class="toc-text">添加一个子项目Hystrix实现服务熔断处理</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-5"><span class="toc-number">1.0.1.6.1.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类添加注解-EnableCircuitBreaker"><span class="toc-number">1.0.1.6.1.2.</span> <span class="toc-text">主启动类添加注解@EnableCircuitBreaker</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在controller中添加应对熔断的方法hystrixGet"><span class="toc-number">1.0.1.6.1.3.</span> <span class="toc-text">在controller中添加应对熔断的方法hystrixGet</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法"><span class="toc-number">1.0.1.6.1.4.</span> <span class="toc-text">同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#服务降级"><span class="toc-number">1.0.1.7.</span> <span class="toc-text">服务降级</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#在springcloud-api中"><span class="toc-number">1.0.1.7.1.</span> <span class="toc-text">在springcloud-api中</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在service包下，创建一个UserServiceFallback类，记得要加-Component注解"><span class="toc-number">1.0.1.7.1.1.</span> <span class="toc-text">在service包下，创建一个UserServiceFallback类，记得要加@Component注解</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在service中，修改UserService代码，添加回调类"><span class="toc-number">1.0.1.7.1.2.</span> <span class="toc-text">在service中，修改UserService代码，添加回调类</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在客户端，yml添加配置开启降级"><span class="toc-number">1.0.1.7.1.3.</span> <span class="toc-text">在客户端，yml添加配置开启降级</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#然后在启动类中扫描包，-SpringBootApplication一定要扫描api下的包和本地的包，不然会报错"><span class="toc-number">1.0.1.7.1.4.</span> <span class="toc-text">然后在启动类中扫描包，@SpringBootApplication一定要扫描api下的包和本地的包，不然会报错</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#测试-1"><span class="toc-number">1.0.1.7.1.5.</span> <span class="toc-text">测试</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。"><span class="toc-number">1.0.1.7.1.6.</span> <span class="toc-text">可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Hystrix-Dashboard流监控"><span class="toc-number">1.0.1.8.</span> <span class="toc-text">Hystrix Dashboard流监控</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#新建一个监控面板springcloud-hystrix-dashboard"><span class="toc-number">1.0.1.8.1.</span> <span class="toc-text">新建一个监控面板springcloud-hystrix-dashboard</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#设定为9001端口"><span class="toc-number">1.0.1.8.1.1.</span> <span class="toc-text">设定为9001端口</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类添加注解-EnableHystrixDashboard"><span class="toc-number">1.0.1.8.1.2.</span> <span class="toc-text">主启动类添加注解@EnableHystrixDashboard</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#在服务提供者中"><span class="toc-number">1.0.1.8.2.</span> <span class="toc-text">在服务提供者中</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-6"><span class="toc-number">1.0.1.8.2.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在启动类中添加Bean"><span class="toc-number">1.0.1.8.2.2.</span> <span class="toc-text">在启动类中添加Bean</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#测试-2"><span class="toc-number">1.0.1.8.3.</span> <span class="toc-text">测试</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#启动Eureka注册中心"><span class="toc-number">1.0.1.8.3.1.</span> <span class="toc-text">启动Eureka注册中心</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#启动服务提供者"><span class="toc-number">1.0.1.8.3.2.</span> <span class="toc-text">启动服务提供者</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#启动Hystrix监控面板"><span class="toc-number">1.0.1.8.3.3.</span> <span class="toc-text">启动Hystrix监控面板</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#打开http-localhost-9001-hystrix"><span class="toc-number">1.0.1.8.3.4.</span> <span class="toc-text">打开http://localhost:9001/hystrix/</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#输入服务提供者的url-actuator-hystrix-stream，如http-localhost-8001-actuator-hystrix-stream"><span class="toc-number">1.0.1.8.3.5.</span> <span class="toc-text">输入服务提供者的url+/actuator/hystrix.stream，如http://localhost:8001/actuator/hystrix.stream</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#监控成功"><span class="toc-number">1.0.1.8.3.6.</span> <span class="toc-text">监控成功</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Zuul路由网关"><span class="toc-number">1.0.1.9.</span> <span class="toc-text">Zuul路由网关</span></a></li></ol></li></ol></li></ol></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        微服务架构之SpringCloud
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">ECIN'S BLOG</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-11-26T00:38:51.000Z" itemprop="datePublished">2019-11-26</time>
        
      
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/Java/" rel="tag">Java</a>, <a class="tag-link" href="/tags/SpringBoot/" rel="tag">SpringBoot</a>, <a class="tag-link" href="/tags/SpringCloud/" rel="tag">SpringCloud</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="微服务架构之SpringCloud"><a href="#微服务架构之SpringCloud" class="headerlink" title="微服务架构之SpringCloud"></a>微服务架构之SpringCloud</h1><h4 id="学习前提"><a href="#学习前提" class="headerlink" title="学习前提"></a>学习前提</h4><ul>
<li>JavaSE –(Java基础)</li>
<li>Mysql</li>
<li>Servlet</li>
<li>Http</li>
<li>Mybatis</li>
<li>Spring</li>
<li>SpringMVC</li>
<li>SpringBoot</li>
<li>Dubbo Zookeeper 分布式基础</li>
<li>Maven Git</li>
<li>Ajax Json </li>
<li>AND　SO　ＯＮ</li>
</ul>
<p>有了以上基础，那么可以学习SpringCloud了</p>
<h4 id="微服务架构四个核心问题"><a href="#微服务架构四个核心问题" class="headerlink" title="微服务架构四个核心问题"></a>微服务架构四个核心问题</h4><ul>
<li>服务众多 客户端该如何访问</li>
<li>服务之间的通信方式（Dubbo－RPC框架　SpringCloud－REST接口方式）</li>
<li>如何治理服务</li>
<li>服务挂了该怎么办</li>
</ul>
<h6 id="解决方案"><a href="#解决方案" class="headerlink" title="解决方案"></a>解决方案</h6><p>Spring Cloud　NetFlix</p>
<p>Apache　Dubbo　Zookeeper</p>
<p>Spring Cloud Alibaba</p>
<h4 id="微服务与微服务架构"><a href="#微服务与微服务架构" class="headerlink" title="微服务与微服务架构"></a>微服务与微服务架构</h4><h5 id="微服务"><a href="#微服务" class="headerlink" title="微服务"></a>微服务</h5><ul>
<li>强调的是服务的大小，它关注的是一个点，是具体解决某一个问题提供的对应服务的一个服务应用</li>
<li>比如，IDEA中的一个微服务工程，或是一个MOUDLE</li>
</ul>
<h5 id="微服务架构"><a href="#微服务架构" class="headerlink" title="微服务架构"></a>微服务架构</h5><ul>
<li>一种架构模式，一种架构风格，它提倡将单一的应用程序划分为一组小的服务，服务之间相互协调相互配合。</li>
<li>每一个服务运行在其独立的进程中，服务与服务之间采用轻量级的通信机制互相协作，每个服务都围绕着具体的业务进行构建，并且能够被<strong>独立</strong>地部署到生产环境中，另外，应尽量避免统一的，集中式的服务管理机制，对具体的一个服务而言，应根据业务上下文，选择合适的语言，工具对其进行构建。</li>
</ul>
<h3 id="SpringCloud-Begin"><a href="#SpringCloud-Begin" class="headerlink" title="SpringCloud Begin"></a>SpringCloud Begin</h3><h4 id="基础项目搭建"><a href="#基础项目搭建" class="headerlink" title="基础项目搭建"></a>基础项目搭建</h4><h5 id="父项目"><a href="#父项目" class="headerlink" title="父项目"></a>父项目</h5><p>新建一个空的Maven项目，导入以下配置及依赖，在<dependencyManagement>中添加的版本，在子工程中可以不用管理版本，打包方式为pom</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line">    <span class="tag">&lt;<span class="name">packaging</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">packaging</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">properties</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">spring-cloud-version</span>&gt;</span>Finchley.SR2<span class="tag">&lt;/<span class="name">spring-cloud-version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">spring-boot-version</span>&gt;</span>2.1.4.RELEASE<span class="tag">&lt;/<span class="name">spring-boot-version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">project.build.sourceEncoding</span>&gt;</span>UTF-8<span class="tag">&lt;/<span class="name">project.build.sourceEncoding</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">maven.compiler.source</span>&gt;</span>1.8<span class="tag">&lt;/<span class="name">maven.compiler.source</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">maven.compiler.target</span>&gt;</span>1.8<span class="tag">&lt;/<span class="name">maven.compiler.target</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">junit.version</span>&gt;</span>4.12<span class="tag">&lt;/<span class="name">junit.version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">lombok.version</span>&gt;</span>1.16.10<span class="tag">&lt;/<span class="name">lombok.version</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">log4j.version</span>&gt;</span>1.2.17<span class="tag">&lt;/<span class="name">log4j.version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-dependencies<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring-boot-version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-dependencies<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;spring-cloud-version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">type</span>&gt;</span>pom<span class="tag">&lt;/<span class="name">type</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">scope</span>&gt;</span>import<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--数据库--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>5.1.47<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--Druid--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.1.10<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--SpringBoot启动器--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.3.2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">&lt;!--日志测试相关依赖--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>ch.qos.logback<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>logback-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.2.3<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--Lomok--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.projectlombok<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>lombok<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;lombok.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--log4j--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>log4j<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;log4j.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--junit--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">version</span>&gt;</span>$&#123;junit.version&#125;<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">scope</span>&gt;</span>test<span class="tag">&lt;/<span class="name">scope</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependencyManagement</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">project</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>注意，SpringCloud的版本必须与对应的SpringBoot版本对应</strong></p>
<h5 id="写一个api提供者"><a href="#写一个api提供者" class="headerlink" title="写一个api提供者"></a>写一个api提供者</h5><p>然后创建一个子工程，用于放置api</p>
<p>创建一个实体类User，需要注意的是要实现序列化</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.ecin520.api.entity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.io.Serializable;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> Integer id;</span><br><span class="line">  <span class="keyword">private</span> String name;</span><br><span class="line">  <span class="keyword">private</span> Integer age;</span><br><span class="line">  <span class="comment">/**</span></span><br><span class="line"><span class="comment">   * 这个表示该数据在哪个数据库，微服务一个服务对应一个数据库，同一个信息可能存在不同数据库</span></span><br><span class="line"><span class="comment">   * */</span></span><br><span class="line">  <span class="keyword">private</span> String dbname;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Integer <span class="title">getId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> id;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.id = id;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">getDbname</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> dbname;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setDbname</span><span class="params">(String dbname)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.dbname = dbname;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> name;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.name = name;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> Integer <span class="title">getAge</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> age;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setAge</span><span class="params">(Integer age)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.age = age;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样就可以通过pom的导入方式在另一个子工程中引用api里的类了</p>
<h5 id="创建一个服务提供者，添加依赖"><a href="#创建一个服务提供者，添加依赖" class="headerlink" title="创建一个服务提供者，添加依赖"></a>创建一个服务提供者，添加依赖</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--拿到api--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.ecin520<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springcloud-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>junit<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>mysql<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mysql-connector-java<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.alibaba<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>druid<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--完善监控信息--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>



<p>创建一个数据库springcloud_db1</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">-- ----------------------------</span><br><span class="line">-- Table structure for user</span><br><span class="line">-- ----------------------------</span><br><span class="line">DROP TABLE IF EXISTS `user`;</span><br><span class="line">CREATE TABLE `user`  (</span><br><span class="line">  `id` int(11) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `name` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,</span><br><span class="line">  `age` int(11) NULL DEFAULT NULL,</span><br><span class="line">  `dbname` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,</span><br><span class="line">  PRIMARY KEY (`id`) USING BTREE</span><br><span class="line">) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = &apos;用户表&apos; ROW_FORMAT = Dynamic;</span><br><span class="line"></span><br><span class="line">-- ----------------------------</span><br><span class="line">-- Records of user</span><br><span class="line">-- ----------------------------</span><br><span class="line">INSERT INTO `user`(`name`, `age`, dbname) VALUES (&apos;郭伟&apos;, 23, DATABASE());</span><br><span class="line"></span><br><span class="line">SET FOREIGN_KEY_CHECKS = 1;</span><br><span class="line"></span><br><span class="line">SELECT * FROM user;</span><br></pre></td></tr></table></figure>

<p>写一个接口UserDao</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Mapper</span></span><br><span class="line"><span class="meta">@Repository</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserDao</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span></span>;</span><br><span class="line">    <span class="function">User <span class="title">getUserById</span><span class="params">(Integer id)</span></span>;</span><br><span class="line">    <span class="function">List&lt;User&gt; <span class="title">listAllUsers</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Mybatis配置，在resource下新建包mybatis及其目录下新建mapper（xml映射）</p>
<p><img src="http://148.70.50.70/source/1574691503030.jpg" alt="image.png"></p>
<p>yml中添加配置</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mybatis配置</span></span><br><span class="line"><span class="attr">mybatis:</span></span><br><span class="line"><span class="attr">  type-aliases-package:</span> <span class="string">com.ecin520.api.entity</span></span><br><span class="line"><span class="attr">  mapper-locations:</span> <span class="attr">classpath:mybatis/mapper/*.xml</span></span><br><span class="line"><span class="attr">  config-location:</span> <span class="attr">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Spring配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">springcloud-provider-user</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">org.gjt.mm.mysql.Driver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:mysql://localhost:3306/springcloud_db1?useUnicode=true&amp;characterEncoding=utf-8</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr">    password:</span> <span class="string">qwer1234</span></span><br></pre></td></tr></table></figure>

<p>mybatis-config可以先不用配置，或者配置下启用缓存</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line">        <span class="meta">&lt;!DOCTYPE configuration</span></span><br><span class="line"><span class="meta">                PUBLIC "-//mybatis.org//DTD Config 3.0//EN"</span></span><br><span class="line"><span class="meta">                "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">configuration</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">settings</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">setting</span> <span class="attr">name</span>=<span class="string">"cacheEnabled"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">settings</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">configuration</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>写一下UserMapper的数据库增删改查语句</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;</span></span><br><span class="line"><span class="meta">&lt;!DOCTYPE mapper</span></span><br><span class="line"><span class="meta">  PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">mapper</span> <span class="attr">namespace</span>=<span class="string">"com.ecin520.provider.dao.UserDao"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">insert</span> <span class="attr">id</span>=<span class="string">"insertUser"</span> <span class="attr">parameterType</span>=<span class="string">"user"</span>&gt;</span></span><br><span class="line">        INSERT INTO user (name, age, dbname) VALUES (#&#123;name&#125;, #&#123;age&#125;, DATABASE());</span><br><span class="line">    <span class="tag">&lt;/<span class="name">insert</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"getUserById"</span> <span class="attr">resultType</span>=<span class="string">"user"</span> <span class="attr">parameterType</span>=<span class="string">"int"</span>&gt;</span></span><br><span class="line">        SELECT * FROM user WHERE id = #&#123;id&#125;;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">select</span> <span class="attr">id</span>=<span class="string">"listAllUsers"</span> <span class="attr">resultType</span>=<span class="string">"user"</span>&gt;</span></span><br><span class="line">        SELECT * FROM user;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">select</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">mapper</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>开始写service，先写接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span></span>;</span><br><span class="line">    <span class="function">User <span class="title">getUserById</span><span class="params">(Integer id)</span></span>;</span><br><span class="line">    <span class="function">List&lt;User&gt; <span class="title">listAllUsers</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>实现接口</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Service</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceImpl</span> <span class="keyword">implements</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> UserDao userDao;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.insertUser(user);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> User <span class="title">getUserById</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.getUserById(id);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">listAllUsers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> userDao.listAllUsers();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="创建消费者consumer"><a href="#创建消费者consumer" class="headerlink" title="创建消费者consumer"></a>创建消费者consumer</h5><h6 id="简单配置pom"><a href="#简单配置pom" class="headerlink" title="简单配置pom"></a>简单配置pom</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--拿到api--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.ecin520<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springcloud-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="添加配置，创建一个REST配置类RestConfig"><a href="#添加配置，创建一个REST配置类RestConfig" class="headerlink" title="添加配置，创建一个REST配置类RestConfig"></a>添加配置，创建一个REST配置类RestConfig</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 配置RestBean</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RestConfig</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Bean</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> RestTemplate <span class="title">restTemplate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> RestTemplate();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="创建一个UserConsumerController类"><a href="#创建一个UserConsumerController类" class="headerlink" title="创建一个UserConsumerController类"></a>创建一个UserConsumerController类</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserConsumerController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Autowired</span></span><br><span class="line">  <span class="keyword">private</span> RestTemplate restTemplate;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String REST_URL_PREFIX =<span class="string">"http://localhost:8001"</span>;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/&#123;id&#125;"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> User <span class="title">getUserById</span><span class="params">(@PathVariable(<span class="string">"id"</span>)</span> Integer id) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> restTemplate.getForObject(REST_URL_PREFIX+<span class="string">"/user/getUserById/"</span>+id, User.class);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/insertUser"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> restTemplate.postForObject(REST_URL_PREFIX+<span class="string">"/user/insertUser"</span>,user,Boolean.class);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/listAllUsers"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">listAllUsers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> restTemplate.getForObject(REST_URL_PREFIX+<span class="string">"/user/listAllUsers"</span>, List.class);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="application-yml配置端口为8080，启动provider提供者，然后访问"><a href="#application-yml配置端口为8080，启动provider提供者，然后访问" class="headerlink" title="application.yml配置端口为8080，启动provider提供者，然后访问"></a>application.yml配置端口为8080，启动provider提供者，然后访问</h6><figure class="highlight http"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">http://localhost:8080/consumer/user/listAllUsers</span></span><br></pre></td></tr></table></figure>

<p>访问成功则说明基础项目搭建成功</p>
<h4 id="添加Eureka服务注册中心"><a href="#添加Eureka服务注册中心" class="headerlink" title="添加Eureka服务注册中心"></a>添加Eureka服务注册中心</h4><p>上面的基础项目是直接使用RestTemplate调用provider提供的接口，但是并没有一个服务注册中心，这里我们使用Eureka作为服务注册中心，提供一个管理服务的平台。</p>
<p>什么是Eureka，Eureka是Netflix开发的基于REST服务的服务注册与发现的组件，它属于CS架构。</p>
<p>Eureka Client：客户端，与Eureka Server交互</p>
<p>Eureka Server：服务端，提供服务注册和发现的能力（微服务的注册中心）</p>
<h5 id="修改一下host文件模拟不同地址"><a href="#修改一下host文件模拟不同地址" class="headerlink" title="修改一下host文件模拟不同地址"></a>修改一下host文件模拟不同地址</h5><figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">C:\<span class="title">Windows</span>\<span class="title">System32</span>\<span class="title">drivers</span>\<span class="title">etc</span></span></span><br></pre></td></tr></table></figure>

<p><img src="http://148.70.50.70/source/1574694996433.jpg" alt="image.png"></p>
<p>使用管理员权限修改hosts文件</p>
<h5 id="创建一个Eureka服务端"><a href="#创建一个Eureka服务端" class="headerlink" title="创建一个Eureka服务端"></a>创建一个Eureka服务端</h5><h6 id="添加依赖"><a href="#添加依赖" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka-server<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="配置yml"><a href="#配置yml" class="headerlink" title="配置yml"></a>配置yml</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span></span><br><span class="line">      <span class="string">eureka-server1</span></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">7001</span></span><br><span class="line"></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    hostname:</span> <span class="string">eureka7001.com</span> <span class="comment">#Eureka服务端的名字</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    register-with-eureka:</span> <span class="literal">false</span> <span class="comment"># 表示是否向Eureka注册自己</span></span><br><span class="line"><span class="attr">    fetch-registry:</span> <span class="literal">false</span> <span class="comment"># 如果为false，表示自己为注册中心</span></span><br><span class="line"><span class="attr">    service-url:</span> <span class="attr">http://eureka7001.com:7001/eureka/</span></span><br></pre></td></tr></table></figure>

<h6 id="再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端"><a href="#再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端" class="headerlink" title="再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端"></a>再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 启动类，在服务启动后自动注册到Eureka中</span></span><br><span class="line"><span class="comment"> * 添加服务发现注解</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableEurekaClient</span></span><br><span class="line"><span class="meta">@EnableDiscoveryClient</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ProviderApplication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(ProviderApplication.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="也可以在Controller中写一下获取微服务实例的代码"><a href="#也可以在Controller中写一下获取微服务实例的代码" class="headerlink" title="也可以在Controller中写一下获取微服务实例的代码"></a>也可以在Controller中写一下获取微服务实例的代码</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Autowired</span></span><br><span class="line"><span class="keyword">private</span> DiscoveryClient discoveryClient;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 注册进来的微服务，从注册中心获取一些信息</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/listServices"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> Object <span class="title">discovery</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="comment">// 获取微服务的列表清单</span></span><br><span class="line">  List&lt;String&gt; services = discoveryClient.getServices();</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 获取具体微服务信息</span></span><br><span class="line">  List&lt;ServiceInstance&gt; instances = discoveryClient.getInstances(<span class="string">"SPRINGCLOUD-PROVIDER-USER"</span>);</span><br><span class="line"></span><br><span class="line">  instances.forEach((instance)-&gt;&#123;</span><br><span class="line">    System.out.println(instance.getHost());</span><br><span class="line">    System.out.println(instance.getPort());</span><br><span class="line">    System.out.println(instance.getUri());</span><br><span class="line">    System.out.println(instance.getServiceId());</span><br><span class="line">  &#125;);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>.discoveryClient;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="最重要的就是配置yml，在之前的yml中添加，最重要的就是spring-application-name（这个会是一个服务标识）和eureka-client-service-url（这是注册中心的地址）"><a href="#最重要的就是配置yml，在之前的yml中添加，最重要的就是spring-application-name（这个会是一个服务标识）和eureka-client-service-url（这是注册中心的地址）" class="headerlink" title="最重要的就是配置yml，在之前的yml中添加，最重要的就是spring.application.name（这个会是一个服务标识）和eureka.client.service-url（这是注册中心的地址）"></a>最重要的就是配置yml，在之前的yml中添加，最重要的就是spring.application.name（这个会是一个服务标识）和eureka.client.service-url（这是注册中心的地址）</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mybatis配置</span></span><br><span class="line"><span class="attr">mybatis:</span></span><br><span class="line"><span class="attr">  type-aliases-package:</span> <span class="string">com.ecin520.api.entity</span></span><br><span class="line"><span class="attr">  mapper-locations:</span> <span class="attr">classpath:mybatis/mapper/*.xml</span></span><br><span class="line"><span class="attr">  config-location:</span> <span class="attr">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Spring配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">springcloud-provider-user</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">org.gjt.mm.mysql.Driver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:mysql://localhost:3306/springcloud_db1?useUnicode=true&amp;characterEncoding=utf-8</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr">    password:</span> <span class="string">qwer1234</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置,服务注册到哪里</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line">      <span class="comment"># 向Eureka集群注册中心提供服务</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    instance-id:</span> <span class="string">springcloud-provider-8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># info配置,这里可以配置一下一些信息,有无都无所谓</span></span><br><span class="line"><span class="attr">info:</span></span><br><span class="line">  <span class="string">app.name:</span> <span class="string">ecin520's</span> <span class="string">springcloud</span> <span class="string">project</span></span><br><span class="line">  <span class="string">company.name:</span> <span class="string">ecin520</span></span><br></pre></td></tr></table></figure>

<h6 id="首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务"><a href="#首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务" class="headerlink" title="首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务"></a>首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务</h6><p><img src="http://148.70.50.70/source/1574696001295.jpg" alt="image.png"></p>
<h4 id="配置Eureka集群环境"><a href="#配置Eureka集群环境" class="headerlink" title="配置Eureka集群环境"></a>配置Eureka集群环境</h4><p>以上的配置在本质上还是单机的，上面已经通过修改hosts文件实现了模拟不同地址，接下来开始配置集群环境。</p>
<h5 id="首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003"><a href="#首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003" class="headerlink" title="首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003"></a>首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003</h5><h6 id="添加依赖-1"><a href="#添加依赖-1" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka-server<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="主启动类配置注解"><a href="#主启动类配置注解" class="headerlink" title="主启动类配置注解"></a>主启动类配置注解</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">@SpringBootApplication</span><br><span class="line">@EnableEurekaServer</span><br><span class="line">public class EurekaApplication_7002 &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">      SpringApplication.run(EurekaApplication_7002.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="配置yml-1"><a href="#配置yml-1" class="headerlink" title="配置yml"></a>配置yml</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span></span><br><span class="line">      <span class="string">eureka-server1</span></span><br><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">7001</span></span><br><span class="line"></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    hostname:</span> <span class="string">eureka7001.com</span> <span class="comment">#Eureka服务端的名字</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    register-with-eureka:</span> <span class="literal">false</span> <span class="comment"># 表示是否向Eureka注册自己</span></span><br><span class="line"><span class="attr">    fetch-registry:</span> <span class="literal">false</span> <span class="comment"># 如果为false，表示自己为注册中心</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/</span></span><br></pre></td></tr></table></figure>

<h6 id="7002的配置"><a href="#7002的配置" class="headerlink" title="7002的配置"></a>7002的配置</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/</span></span><br></pre></td></tr></table></figure>

<h6 id="7003"><a href="#7003" class="headerlink" title="7003"></a>7003</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/</span></span><br></pre></td></tr></table></figure>

<h5 id="修改provider，把服务提供到的注册中心由一个增加到三个"><a href="#修改provider，把服务提供到的注册中心由一个增加到三个" class="headerlink" title="修改provider，把服务提供到的注册中心由一个增加到三个"></a>修改provider，把服务提供到的注册中心由一个增加到三个</h5><h6 id="在yml中"><a href="#在yml中" class="headerlink" title="在yml中"></a>在yml中</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mybatis配置</span></span><br><span class="line"><span class="attr">mybatis:</span></span><br><span class="line"><span class="attr">  type-aliases-package:</span> <span class="string">com.ecin520.api.entity</span></span><br><span class="line"><span class="attr">  mapper-locations:</span> <span class="attr">classpath:mybatis/mapper/*.xml</span></span><br><span class="line"><span class="attr">  config-location:</span> <span class="attr">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Spring配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">springcloud-provider-user</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">org.gjt.mm.mysql.Driver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:mysql://localhost:3306/springcloud_db1?useUnicode=true&amp;characterEncoding=utf-8</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr">    password:</span> <span class="string">qwer1234</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置,服务注册到哪里</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line">      <span class="comment"># 向Eureka集群注册中心提供服务</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    instance-id:</span> <span class="string">springcloud-provider-8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># info配置</span></span><br><span class="line"><span class="attr">info:</span></span><br><span class="line">  <span class="string">app.name:</span> <span class="string">ecin520's</span> <span class="string">springcloud</span> <span class="string">project</span></span><br><span class="line">  <span class="string">company.name:</span> <span class="string">ecin520</span></span><br></pre></td></tr></table></figure>

<p>这样实现了注册中心的关联，如果7001注册中心挂掉，则还有7002和7003继续支撑</p>
<h5 id="Eureka熔断机制"><a href="#Eureka熔断机制" class="headerlink" title="Eureka熔断机制"></a>Eureka熔断机制</h5><h6 id="自我保护机制"><a href="#自我保护机制" class="headerlink" title="自我保护机制"></a>自我保护机制</h6><ul>
<li>默认情况下，如果EurekaServer在一 定时间内没有接收到某 个微服务实例的心跳，EurekaServer将会注销该实例(默认90秒)。但是当网络分区故障发生时，微服务与Eureka之间无法正常通行，以上行为可能变得非常危险了-因为微服务本身其实是健康的，此时本不应该注销这个服务。Eureka通过 自我保护机制来解决这个问题-当EurekaServer节点在短时间内丢失过多客户端时(可能发生了网络分区故障)，那么这个节点就会进入自我保护模式。一旦进入该模式，EurekaServer就会保护服务注册表中的信息，不再删除服务注册表中的数据(也就是不会注销任何微服务)。当网络故障恢复后，该EurekaServer节 点会自动退出自我保护模式。</li>
<li>在自我保护模式中，EurekaServer会保护服务注册表中的信息，不再注销任何服务实例。当它收到的心跳数重新恢复到阈值以上时，该EurekaServer节 点就会自动退出自我保护模式。它的设计哲学就是宁可保留错误的服务注册信息，也不盲目注销任何可能健康的服务实例。</li>
<li>综上，自我保护模式是一种应对网络异常的安全保护措施。它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的微服务都会保留)，也不盲目注销任何健康的微服务。使用自我保护模式，可以让Eureka集群更加的健壮和稳定</li>
<li>在SpringCloud中，可以使用eureka.server.enable-self-preservation = false 禁用自我保护模式[不推荐关闭自我保护机制]</li>
</ul>
<h6 id="CAP原则"><a href="#CAP原则" class="headerlink" title="CAP原则"></a>CAP原则</h6><ul>
<li>C（Consistency）强一致性</li>
<li>A（Avaliability）可用性</li>
<li>P（Partition tolerance）分区容错性</li>
</ul>
<p><strong>CAP的三进二：CA AP CP</strong></p>
<p><img src="http://148.70.50.70/source/1574697283154.jpg" alt="image.png"></p>
<p><img src="http://148.70.50.70/source/1574697315323.jpg" alt="image.png"></p>
<h4 id="Ribben负载均衡"><a href="#Ribben负载均衡" class="headerlink" title="Ribben负载均衡"></a>Ribben负载均衡</h4><p>什么是负载均衡？假设我们有三个服务提供者同时想注册中心提供相同的服务，但是使用服务的时候要是一直访问同一个服务，其他两个服务就会非常闲置并且有可能第一个服务已经承受不了压力了。负载均衡就是让消费者不仅仅是访问某一个服务，可以使用轮询，随机等实现不同的负载均衡，当然也可以自己实现接口自己去写负载均衡算法。</p>
<p>Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具，它基于Netflix Ribbon实现。通过Spring Cloud的封装，可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的服务调用。Spring Cloud Ribbon虽然只是一个工具类框架，它不像服务注册中心、配置中心、API网关那样需要独立部署，但是它几乎存在于每一个Spring Cloud构建的微服务和基础设施中。</p>
<h5 id="修改consumer"><a href="#修改consumer" class="headerlink" title="修改consumer"></a>修改consumer</h5><h6 id="在上面基础的配置下添加"><a href="#在上面基础的配置下添加" class="headerlink" title="在上面基础的配置下添加"></a>在上面基础的配置下添加</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-ribbon<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="将原来的UserConsumerController中的URL换成"><a href="#将原来的UserConsumerController中的URL换成" class="headerlink" title="将原来的UserConsumerController中的URL换成"></a>将原来的UserConsumerController中的URL换成</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 若使用ribben，不应写死URL，而是写成注册中心地址</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="comment">// private static final String REST_URL_PREFIX ="http://localhost:8000";</span></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String REST_URL_PREFIX =<span class="string">"http://SPRINGCLOUD-PROVIDER-USER"</span>;</span><br></pre></td></tr></table></figure>

<h6 id="配置注册中心地址（从哪里拿）"><a href="#配置注册中心地址（从哪里拿）" class="headerlink" title="配置注册中心地址（从哪里拿）"></a>配置注册中心地址（从哪里拿）</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8080</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    register-with-eureka:</span> <span class="literal">false</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/</span></span><br></pre></td></tr></table></figure>

<h6 id="在最外层的同级包下创建一个规则配置rule，具体位置"><a href="#在最外层的同级包下创建一个规则配置rule，具体位置" class="headerlink" title="在最外层的同级包下创建一个规则配置rule，具体位置"></a>在最外层的同级包下创建一个规则配置rule，具体位置</h6><p><img src="http://148.70.50.70/source/1574698895388.jpg" alt="image.png"></p>
<h6 id="开启负载均衡，修改RestConfig"><a href="#开启负载均衡，修改RestConfig" class="headerlink" title="开启负载均衡，修改RestConfig"></a>开启负载均衡，修改RestConfig</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="meta">@LoadBalanced</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> RestTemplate <span class="title">restTemplate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> RestTemplate();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h6 id="添加负载均衡随机轮询规则"><a href="#添加负载均衡随机轮询规则" class="headerlink" title="添加负载均衡随机轮询规则"></a>添加负载均衡随机轮询规则</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 实现负载均衡</span></span><br><span class="line"><span class="comment"> * AvailabilityFilteringRule : 会先过滤掉跳闸，访问故障的服务，对剩下的进行轮询</span></span><br><span class="line"><span class="comment"> * RoundRobinRule : 轮询</span></span><br><span class="line"><span class="comment"> * RandomRule : 随机</span></span><br><span class="line"><span class="comment"> * RetryRule : 会先按照轮询获取服务，如果服务获取失败，则会在指定的时间内进行重试</span></span><br><span class="line"><span class="comment"> * ......其他负载均衡算法</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@Configuration</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RandRule</span> </span>&#123;</span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> IRule <span class="title">randomRule</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> RandomRule();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="最后在启动类中添加注解"><a href="#最后在启动类中添加注解" class="headerlink" title="最后在启动类中添加注解"></a>最后在启动类中添加注解</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableEurekaClient</span></span><br><span class="line"><span class="meta">@RibbonClient</span>(name = <span class="string">"SPRINGCLOUD-PROVIDER-USER"</span>, configuration = RandRule.class)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConsumerApplication</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    SpringApplication.run(ConsumerApplication.class, args);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="consumer修改好了后，开始修改provider"><a href="#consumer修改好了后，开始修改provider" class="headerlink" title="consumer修改好了后，开始修改provider"></a>consumer修改好了后，开始修改provider</h5><h6 id="添加两个数据库"><a href="#添加两个数据库" class="headerlink" title="添加两个数据库"></a>添加两个数据库</h6><p><img src="http://148.70.50.70/source/1574699028571.jpg" alt="image.png"></p>
<h6 id="都是同样的内容"><a href="#都是同样的内容" class="headerlink" title="都是同样的内容"></a>都是同样的内容</h6><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">-- ----------------------------</span><br><span class="line">-- Table structure for user</span><br><span class="line">-- ----------------------------</span><br><span class="line">DROP TABLE IF EXISTS `user`;</span><br><span class="line">CREATE TABLE `user`  (</span><br><span class="line">  `id` int(11) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `name` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,</span><br><span class="line">  `age` int(11) NULL DEFAULT NULL,</span><br><span class="line">  `dbname` varchar(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,</span><br><span class="line">  PRIMARY KEY (`id`) USING BTREE</span><br><span class="line">) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = &apos;用户表&apos; ROW_FORMAT = Dynamic;</span><br><span class="line"></span><br><span class="line">-- ----------------------------</span><br><span class="line">-- Records of user</span><br><span class="line">-- ----------------------------</span><br><span class="line">INSERT INTO `user`(`name`, `age`, dbname) VALUES (&apos;郭伟&apos;, 23, DATABASE());</span><br><span class="line"></span><br><span class="line">SET FOREIGN_KEY_CHECKS = 1;</span><br><span class="line"></span><br><span class="line">SELECT * FROM user;</span><br></pre></td></tr></table></figure>

<h6 id="添加两个服务提供者provider-8002-provider-8003"><a href="#添加两个服务提供者provider-8002-provider-8003" class="headerlink" title="添加两个服务提供者provider-8002 provider-8003"></a>添加两个服务提供者provider-8002 provider-8003</h6><h6 id="添加依赖-2"><a href="#添加依赖-2" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="修改后的三个provider后的application-yml为"><a href="#修改后的三个provider后的application-yml为" class="headerlink" title="修改后的三个provider后的application.yml为"></a>修改后的三个provider后的application.yml为</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mybatis配置</span></span><br><span class="line"><span class="attr">mybatis:</span></span><br><span class="line"><span class="attr">  type-aliases-package:</span> <span class="string">com.ecin520.api.entity</span></span><br><span class="line"><span class="attr">  mapper-locations:</span> <span class="attr">classpath:mybatis/mapper/*.xml</span></span><br><span class="line"><span class="attr">  config-location:</span> <span class="attr">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Spring配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">springcloud-provider-user</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">org.gjt.mm.mysql.Driver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:mysql://localhost:3306/springcloud_db1?useUnicode=true&amp;characterEncoding=utf-8</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr">    password:</span> <span class="string">qwer1234</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置,服务注册到哪里</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line">      <span class="comment"># 向Eureka集群注册中心提供服务</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    instance-id:</span> <span class="string">springcloud-provider-8001</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># info配置</span></span><br><span class="line"><span class="attr">info:</span></span><br><span class="line">  <span class="string">app.name:</span> <span class="string">ecin520's</span> <span class="string">springcloud</span> <span class="string">project</span></span><br><span class="line">  <span class="string">company.name:</span> <span class="string">ecin520</span></span><br></pre></td></tr></table></figure>

<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8002</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mybatis配置</span></span><br><span class="line"><span class="attr">mybatis:</span></span><br><span class="line"><span class="attr">  type-aliases-package:</span> <span class="string">com.ecin520.api.entity</span></span><br><span class="line"><span class="attr">  mapper-locations:</span> <span class="attr">classpath:mybatis/mapper/*.xml</span></span><br><span class="line"><span class="attr">  config-location:</span> <span class="attr">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Spring配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">springcloud-provider-user</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">org.gjt.mm.mysql.Driver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:mysql://localhost:3306/springcloud_db2?useUnicode=true&amp;characterEncoding=utf-8</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr">    password:</span> <span class="string">qwer1234</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置,服务注册到哪里</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line">      <span class="comment"># 向Eureka集群注册中心提供服务</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    instance-id:</span> <span class="string">springcloud-provider-8002</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># info配置</span></span><br><span class="line"><span class="attr">info:</span></span><br><span class="line">  <span class="string">app.name:</span> <span class="string">ecin520's</span> <span class="string">springcloud</span> <span class="string">project</span></span><br><span class="line">  <span class="string">company.name:</span> <span class="string">ecin520</span></span><br></pre></td></tr></table></figure>

<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8003</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># mybatis配置</span></span><br><span class="line"><span class="attr">mybatis:</span></span><br><span class="line"><span class="attr">  type-aliases-package:</span> <span class="string">com.ecin520.api.entity</span></span><br><span class="line"><span class="attr">  mapper-locations:</span> <span class="attr">classpath:mybatis/mapper/*.xml</span></span><br><span class="line"><span class="attr">  config-location:</span> <span class="attr">classpath:mybatis/mybatis-config.xml</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Spring配置</span></span><br><span class="line"><span class="attr">spring:</span></span><br><span class="line"><span class="attr">  application:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">springcloud-provider-user</span></span><br><span class="line"><span class="attr">  datasource:</span></span><br><span class="line"><span class="attr">    type:</span> <span class="string">com.alibaba.druid.pool.DruidDataSource</span></span><br><span class="line"><span class="attr">    driver-class-name:</span> <span class="string">org.gjt.mm.mysql.Driver</span></span><br><span class="line"><span class="attr">    url:</span> <span class="attr">jdbc:mysql://localhost:3306/springcloud_db3?useUnicode=true&amp;characterEncoding=utf-8</span></span><br><span class="line"><span class="attr">    username:</span> <span class="string">root</span></span><br><span class="line"><span class="attr">    password:</span> <span class="string">qwer1234</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置,服务注册到哪里</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line">      <span class="comment"># 向Eureka集群注册中心提供服务</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/</span></span><br><span class="line"><span class="attr">  instance:</span></span><br><span class="line"><span class="attr">    instance-id:</span> <span class="string">springcloud-provider-8003</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># info配置</span></span><br><span class="line"><span class="attr">info:</span></span><br><span class="line">  <span class="string">app.name:</span> <span class="string">ecin520's</span> <span class="string">springcloud</span> <span class="string">project</span></span><br><span class="line">  <span class="string">company.name:</span> <span class="string">ecin520</span></span><br></pre></td></tr></table></figure>

<h5 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h5><h6 id="启动一个Eureka，三个provider以及一个consumer"><a href="#启动一个Eureka，三个provider以及一个consumer" class="headerlink" title="启动一个Eureka，三个provider以及一个consumer"></a>启动一个Eureka，三个provider以及一个consumer</h6><p>访问<a href="http://localhost:7001" target="_blank" rel="noopener">http://localhost:7001</a></p>
<p><img src="http://148.70.50.70/source/1574700133565.jpg" alt="image.png"></p>
<p>发现三个相同的服务但是提供者不同的服务已经注册到了注册中心，接下来测试负载均衡，访问<a href="http://localhost:8080/consumer/user/listAllUsers" target="_blank" rel="noopener">http://localhost:8080/consumer/user/listAllUsers</a></p>
<p><img src="http://148.70.50.70/source/1574700380924.jpg" alt="image.png"></p>
<p><img src="http://148.70.50.70/source/1574700389886.jpg" alt="image.png"></p>
<p><img src="http://148.70.50.70/source/1574700400872.jpg" alt="image.png"></p>
<p>发现随机负载均衡已经实现。</p>
<h4 id="feign接口方式调用服务"><a href="#feign接口方式调用服务" class="headerlink" title="feign接口方式调用服务"></a>feign接口方式调用服务</h4><p>除了上述的URL拼接方式实现的服务调用，还有一种方法就是接口方式调用服务，两者都可以使用，但是大多数人习惯面向接口编程，于是feign应运而生。</p>
<h5 id="springcloud-api中"><a href="#springcloud-api中" class="headerlink" title="springcloud-api中"></a>springcloud-api中</h5><h6 id="添加依赖-3"><a href="#添加依赖-3" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-feign<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="添加service-UserService接口"><a href="#添加service-UserService接口" class="headerlink" title="添加service.UserService接口"></a>添加service.UserService接口</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 使用接口调用服务，而不是使用Ribben的链接拼接方式</span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@FeignClient</span>(value = <span class="string">"SPRINGCLOUD-PROVIDER-USER"</span>)</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/&#123;id&#125;"</span>)</span><br><span class="line">    <span class="function">User <span class="title">getUserById</span><span class="params">(@PathVariable(<span class="string">"id"</span>)</span> Integer id)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/listAllUsers"</span>)</span><br><span class="line">    <span class="function">List&lt;User&gt; <span class="title">listAllUser</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/insertUser"</span>)</span><br><span class="line">    <span class="function"><span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="创建一个feign测试项目springcloud-feign"><a href="#创建一个feign测试项目springcloud-feign" class="headerlink" title="创建一个feign测试项目springcloud-feign"></a>创建一个feign测试项目springcloud-feign</h5><h6 id="添加依赖-4"><a href="#添加依赖-4" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--拿到api--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.ecin520<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springcloud-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-feign<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="配置application-yml"><a href="#配置application-yml" class="headerlink" title="配置application.yml"></a>配置application.yml</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">8080</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Eureka配置</span></span><br><span class="line"><span class="attr">eureka:</span></span><br><span class="line"><span class="attr">  client:</span></span><br><span class="line"><span class="attr">    register-with-eureka:</span> <span class="literal">false</span></span><br><span class="line"><span class="attr">    service-url:</span></span><br><span class="line"><span class="attr">      defaultZone:</span> <span class="attr">http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/</span></span><br></pre></td></tr></table></figure>

<h6 id="主启动类，添加扫描包注解"><a href="#主启动类，添加扫描包注解" class="headerlink" title="主启动类，添加扫描包注解"></a>主启动类，添加扫描包注解</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableEurekaClient</span></span><br><span class="line"><span class="meta">@EnableFeignClients</span>(basePackages = &#123;<span class="string">"com.ecin520.api"</span>&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FeignApplication</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    SpringApplication.run(FeignApplication.class, args);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="创建UserConsumerController类"><a href="#创建UserConsumerController类" class="headerlink" title="创建UserConsumerController类"></a>创建UserConsumerController类</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RestController</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserConsumerController</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@Autowired</span></span><br><span class="line">  <span class="keyword">private</span> UserService userService;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/&#123;id&#125;"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> User <span class="title">getUserById</span><span class="params">(@PathVariable(<span class="string">"id"</span>)</span> Integer id) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userService.getUserById(id);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/insertUser"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userService.insertUser(user);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="meta">@RequestMapping</span>(<span class="string">"/consumer/user/listAllUsers"</span>)</span><br><span class="line">  <span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">listAllUsers</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> userService.listAllUser();</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="配置一下RestConfig"><a href="#配置一下RestConfig" class="headerlink" title="配置一下RestConfig"></a>配置一下RestConfig</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Bean</span></span><br><span class="line"><span class="meta">@LoadBalanced</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> RestTemplate <span class="title">restTemplate</span><span class="params">()</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> RestTemplate();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="其实和上面的consumer是一样的，只不过有些许地方不太一样"><a href="#其实和上面的consumer是一样的，只不过有些许地方不太一样" class="headerlink" title="其实和上面的consumer是一样的，只不过有些许地方不太一样"></a>其实和上面的consumer是一样的，只不过有些许地方不太一样</h6><p>然后就可以测试了，先启动注册中心，然后启动provider，最后启动springcloud-feign，访问接口</p>
<p><a href="http://localhost:8080/consumer/user/listAllUsers" target="_blank" rel="noopener">http://localhost:8080/consumer/user/listAllUsers</a></p>
<p><img src="http://148.70.50.70/source/1574732949690.jpg" alt="image.png"></p>
<h4 id="服务熔断"><a href="#服务熔断" class="headerlink" title="服务熔断"></a>服务熔断</h4><h6 id="分布式系统面临的问题"><a href="#分布式系统面临的问题" class="headerlink" title="分布式系统面临的问题"></a>分布式系统面临的问题</h6><p>复杂分布式体系结构中应用程序有数十个依赖关系，每个依赖关系在某些时候将不可避免的失败</p>
<h6 id="服务雪崩"><a href="#服务雪崩" class="headerlink" title="服务雪崩"></a>服务雪崩</h6><p>多个微服务之间调用的时候，假设微服务A调用微服务B和微服务C，微服务B和微服务C又调用其他的微服务,这就是所谓的“扇出”，如果扇出的链路上某个微服务的调用响应时间过长或者不可用，对微服务A的调用就会占用越来越多的系统资源，进而引起系统崩溃,所谓的“雪崩效应”。</p>
<p>对于高流量的应用来说，单一的后端依赖可能会导致所有服务器上的所有资源都在几秒中内饱和。比失败更糟糕的是，这些应用程序还可能导致服务之间的延迟增加，备份队列，线程和其他系统资源紧张，导致整个系统发生更多的级联故障，这些都表示需要对故障和延迟进行隔离和管理，以便单个依赖关系的失败，不能取消整个应用程序或系统。</p>
<h6 id="Hystrix"><a href="#Hystrix" class="headerlink" title="Hystrix"></a>Hystrix</h6><p>Hystrix是一个用于处理分布式系统的延迟和容错的开源库， 在分布式系统里，许多依赖不可避免的会调用失败，比如超时，异常等，Hystrix能够保证在一个依赖出问题的情况下，不会导致整体服务失败，避免级联故障,以提高分布式系统的弹性。</p>
<p> “断路器”本身是一种开关装置，当某个服务单元发生故障之后，通过断路器的故障监控(类似熔断保险丝)，向调用方返回一个服务预期的，可处理的<strong>备选响应</strong>(FallBack) ,而不是长时间的等待或者抛出调用方法无法处理的异常，这样就可以保证了服务调用方的线程不会被长时间，不必要的占用，从而避免了故障在分布式系统中的蔓延，乃至雪崩。</p>
<h6 id="Hystrix用处"><a href="#Hystrix用处" class="headerlink" title="Hystrix用处"></a>Hystrix用处</h6><ul>
<li>服务降级</li>
<li>服务熔断</li>
<li>服务限流</li>
<li>接近实时的监控</li>
<li>……</li>
</ul>
<h6 id="服务熔断-1"><a href="#服务熔断-1" class="headerlink" title="服务熔断"></a>服务熔断</h6><p>熔断机制是对应雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务不可用或者响应时间太长时，会进行服务降级，进而熔断该节点微服务的调用，快速返回错误的响应信息。当检测到该节点微服务调用响应正常后回复调用链路，在SpringCloud框架里熔断机制通过Hystrix实现，Hystrix会监控微服务间调用的状况，当失败的调用到一定阈值，缺省是5秒内20次调用失败就会启动熔断机制。熔断机制的注解是@HystrixCommand</p>
<h5 id="添加一个子项目Hystrix实现服务熔断处理"><a href="#添加一个子项目Hystrix实现服务熔断处理" class="headerlink" title="添加一个子项目Hystrix实现服务熔断处理"></a>添加一个子项目Hystrix实现服务熔断处理</h5><h6 id="添加依赖-5"><a href="#添加依赖-5" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-hystrix<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="主启动类添加注解-EnableCircuitBreaker"><a href="#主启动类添加注解-EnableCircuitBreaker" class="headerlink" title="主启动类添加注解@EnableCircuitBreaker"></a>主启动类添加注解@EnableCircuitBreaker</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 启动类，在服务启动后自动注册到Eureka中</span></span><br><span class="line"><span class="comment"> * 添加服务发现注解</span></span><br><span class="line"><span class="comment"> * 添加对熔断的支持<span class="doctag">@EnableCircuitBreaker</span></span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableEurekaClient</span></span><br><span class="line"><span class="meta">@EnableDiscoveryClient</span></span><br><span class="line"><span class="meta">@EnableCircuitBreaker</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HystrixProviderApplication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(HystrixProviderApplication.class, args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="在controller中添加应对熔断的方法hystrixGet"><a href="#在controller中添加应对熔断的方法hystrixGet" class="headerlink" title="在controller中添加应对熔断的方法hystrixGet"></a>在controller中添加应对熔断的方法hystrixGet</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">hystrixGet</span><span class="params">(@PathVariable(<span class="string">"id"</span>)</span>Integer id) </span>&#123;</span><br><span class="line">  User user = <span class="keyword">new</span> User();</span><br><span class="line">  user.setId(id);</span><br><span class="line">  user.setName(<span class="string">"ID不存在，请确认后重试"</span>);</span><br><span class="line">  user.setAge(<span class="keyword">null</span>);</span><br><span class="line">  user.setDbname(<span class="keyword">null</span>);</span><br><span class="line">  <span class="keyword">return</span> user;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法"><a href="#同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法" class="headerlink" title="同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法"></a>同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@RequestMapping</span>(<span class="string">"/getUserById/&#123;id&#125;"</span>)</span><br><span class="line"><span class="meta">@HystrixCommand</span>(fallbackMethod = <span class="string">"hystrixGet"</span>)</span><br><span class="line"><span class="function"><span class="keyword">public</span> User <span class="title">getUserById</span><span class="params">(@PathVariable(<span class="string">"id"</span>)</span>Integer id) </span>&#123;</span><br><span class="line">    User user = userService.getUserById(id);</span><br><span class="line">    <span class="keyword">if</span> (user == <span class="keyword">null</span>) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> RuntimeException(<span class="string">"ID不存在"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> user;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样如果访问一个不存在的ID，就会返回对应的回调方法。</p>
<h4 id="服务降级"><a href="#服务降级" class="headerlink" title="服务降级"></a>服务降级</h4><p>在面对服务器资源紧缺的情况下，有些服务访问较高，有些较低，我们就可以通过服务降级来关闭一些访问较低的服务，从而给访问量高的服务提供更多的资源，在服务降级后，需要返回一个相应的回调类的方法给客户端。</p>
<h5 id="在springcloud-api中"><a href="#在springcloud-api中" class="headerlink" title="在springcloud-api中"></a>在springcloud-api中</h5><h6 id="在service包下，创建一个UserServiceFallback类，记得要加-Component注解"><a href="#在service包下，创建一个UserServiceFallback类，记得要加-Component注解" class="headerlink" title="在service包下，创建一个UserServiceFallback类，记得要加@Component注解"></a>在service包下，创建一个UserServiceFallback类，记得要加@Component注解</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 降级</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserServiceFallback</span> <span class="keyword">implements</span> <span class="title">FallbackFactory</span>&lt;<span class="title">UserService</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> UserService <span class="title">create</span><span class="params">(Throwable throwable)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">new</span> UserService() &#123;</span><br><span class="line">			<span class="meta">@Override</span></span><br><span class="line">			<span class="function"><span class="keyword">public</span> User <span class="title">getUserById</span><span class="params">(Integer id)</span> </span>&#123;</span><br><span class="line">				User user = <span class="keyword">new</span> User();</span><br><span class="line">				user.setId(id);</span><br><span class="line">				user.setName(<span class="string">"ID不存在，请确认后重试，这是降级后的信息"</span>);</span><br><span class="line">				user.setAge(<span class="keyword">null</span>);</span><br><span class="line">				user.setDbname(<span class="keyword">null</span>);</span><br><span class="line">				<span class="keyword">return</span> user;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="meta">@Override</span></span><br><span class="line">			<span class="function"><span class="keyword">public</span> List&lt;User&gt; <span class="title">listAllUser</span><span class="params">()</span> </span>&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">			&#125;</span><br><span class="line"></span><br><span class="line">			<span class="meta">@Override</span></span><br><span class="line">			<span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">				<span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h6 id="在service中，修改UserService代码，添加回调类"><a href="#在service中，修改UserService代码，添加回调类" class="headerlink" title="在service中，修改UserService代码，添加回调类"></a>在service中，修改UserService代码，添加回调类</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@FeignClient</span>(value = <span class="string">"SPRINGCLOUD-PROVIDER-USER"</span>, fallbackFactory = UserServiceFallback.class)</span><br><span class="line"><span class="meta">@Component</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">UserService</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/user/getUserById/&#123;id&#125;"</span>)</span><br><span class="line">	<span class="function">User <span class="title">getUserById</span><span class="params">(@PathVariable(<span class="string">"id"</span>)</span> Integer id)</span>;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/user/listAllUsers"</span>)</span><br><span class="line">	<span class="function">List&lt;User&gt; <span class="title">listAllUser</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">	<span class="meta">@RequestMapping</span>(<span class="string">"/user/insertUser"</span>)</span><br><span class="line">	<span class="function"><span class="keyword">boolean</span> <span class="title">insertUser</span><span class="params">(User user)</span></span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="在客户端，yml添加配置开启降级"><a href="#在客户端，yml添加配置开启降级" class="headerlink" title="在客户端，yml添加配置开启降级"></a>在客户端，yml添加配置开启降级</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 开启降级</span></span><br><span class="line"><span class="attr">feign:</span></span><br><span class="line"><span class="attr">  hystrix:</span></span><br><span class="line"><span class="attr">    enabled:</span> <span class="literal">true</span></span><br></pre></td></tr></table></figure>

<h6 id="然后在启动类中扫描包，-SpringBootApplication一定要扫描api下的包和本地的包，不然会报错"><a href="#然后在启动类中扫描包，-SpringBootApplication一定要扫描api下的包和本地的包，不然会报错" class="headerlink" title="然后在启动类中扫描包，@SpringBootApplication一定要扫描api下的包和本地的包，不然会报错"></a>然后在启动类中扫描包，@SpringBootApplication一定要扫描api下的包和本地的包，不然会报错</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span>(scanBasePackages = &#123;<span class="string">"com.ecin520.consumer"</span>,<span class="string">"com.ecin520.api"</span>&#125;)</span><br><span class="line"><span class="meta">@EnableEurekaClient</span></span><br><span class="line"><span class="meta">@EnableFeignClients</span>(basePackages = &#123;<span class="string">"com.ecin520.api"</span>&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FeignApplication</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    SpringApplication.run(FeignApplication.class, args);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h6 id="测试-1"><a href="#测试-1" class="headerlink" title="测试"></a>测试</h6><p>开启注册中心Eureka后，再打开服务提供者提供服务，再打开用了降级机制的客户端，访问接口<a href="http://localhost:8080/consumer/user/getUserById/2可以正常访问，关掉服务后，再次访问就会出现以下降级后的信息。" target="_blank" rel="noopener">http://localhost:8080/consumer/user/getUserById/2可以正常访问，关掉服务后，再次访问就会出现以下降级后的信息。</a></p>
<p><img src="http://148.70.50.70/source/1574768395963.jpg" alt=""></p>
<h6 id="可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。"><a href="#可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。" class="headerlink" title="可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。"></a>可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。</h6><h4 id="Hystrix-Dashboard流监控"><a href="#Hystrix-Dashboard流监控" class="headerlink" title="Hystrix Dashboard流监控"></a>Hystrix Dashboard流监控</h4><h5 id="新建一个监控面板springcloud-hystrix-dashboard"><a href="#新建一个监控面板springcloud-hystrix-dashboard" class="headerlink" title="新建一个监控面板springcloud-hystrix-dashboard"></a>新建一个监控面板springcloud-hystrix-dashboard</h5><p>导入依赖</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-ribbon<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-hystrix<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-hystrix-dashboard<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="设定为9001端口"><a href="#设定为9001端口" class="headerlink" title="设定为9001端口"></a>设定为9001端口</h6><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">server:</span></span><br><span class="line"><span class="attr">  port:</span> <span class="number">9001</span></span><br></pre></td></tr></table></figure>

<h6 id="主启动类添加注解-EnableHystrixDashboard"><a href="#主启动类添加注解-EnableHystrixDashboard" class="headerlink" title="主启动类添加注解@EnableHystrixDashboard"></a>主启动类添加注解@EnableHystrixDashboard</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableHystrixDashboard</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ConsumerApplication</span> </span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">		SpringApplication.run(ConsumerApplication.class, args);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="在服务提供者中"><a href="#在服务提供者中" class="headerlink" title="在服务提供者中"></a>在服务提供者中</h5><h6 id="添加依赖-6"><a href="#添加依赖-6" class="headerlink" title="添加依赖"></a>添加依赖</h6><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependencies</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--拿到api--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>com.ecin520<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>springcloud-api<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.0-SNAPSHOT<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.mybatis.spring.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>mybatis-spring-boot-starter<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-test<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-web<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-eureka<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-hystrix<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>1.4.6.RELEASE<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="comment">&lt;!--完善监控信息--&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.boot<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-boot-starter-actuator<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.springframework.cloud<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>spring-cloud-starter-netflix-hystrix-dashboard<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependencies</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h6 id="在启动类中添加Bean"><a href="#在启动类中添加Bean" class="headerlink" title="在启动类中添加Bean"></a>在启动类中添加Bean</h6><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 启动类，在服务启动后自动注册到Eureka中</span></span><br><span class="line"><span class="comment"> * 添加服务发现注解</span></span><br><span class="line"><span class="comment"> * 添加对熔断的支持<span class="doctag">@EnableCircuitBreaker</span></span></span><br><span class="line"><span class="comment"> * */</span></span><br><span class="line"><span class="meta">@SpringBootApplication</span></span><br><span class="line"><span class="meta">@EnableEurekaClient</span></span><br><span class="line"><span class="meta">@EnableDiscoveryClient</span></span><br><span class="line"><span class="meta">@EnableCircuitBreaker</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HystrixProviderApplication</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SpringApplication.run(HystrixProviderApplication.class, args);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Bean</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> ServletRegistrationBean <span class="title">servletRegistrationBean</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        ServletRegistrationBean registrationBean = <span class="keyword">new</span> ServletRegistrationBean(<span class="keyword">new</span> HystrixMetricsStreamServlet());</span><br><span class="line">        registrationBean.addUrlMappings(<span class="string">"/actuator/hystrix.stream"</span>);</span><br><span class="line">        <span class="keyword">return</span> registrationBean;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="测试-2"><a href="#测试-2" class="headerlink" title="测试"></a>测试</h5><ul>
<li><h6 id="启动Eureka注册中心"><a href="#启动Eureka注册中心" class="headerlink" title="启动Eureka注册中心"></a>启动Eureka注册中心</h6></li>
<li><h6 id="启动服务提供者"><a href="#启动服务提供者" class="headerlink" title="启动服务提供者"></a>启动服务提供者</h6></li>
<li><h6 id="启动Hystrix监控面板"><a href="#启动Hystrix监控面板" class="headerlink" title="启动Hystrix监控面板"></a>启动Hystrix监控面板</h6></li>
<li><h6 id="打开http-localhost-9001-hystrix"><a href="#打开http-localhost-9001-hystrix" class="headerlink" title="打开http://localhost:9001/hystrix/"></a>打开<a href="http://localhost:9001/hystrix/" target="_blank" rel="noopener">http://localhost:9001/hystrix/</a></h6></li>
</ul>
<p><img src="http://148.70.50.70/source/1574773523980.jpg" alt="image.png"></p>
<ul>
<li><h6 id="输入服务提供者的url-actuator-hystrix-stream，如http-localhost-8001-actuator-hystrix-stream"><a href="#输入服务提供者的url-actuator-hystrix-stream，如http-localhost-8001-actuator-hystrix-stream" class="headerlink" title="输入服务提供者的url+/actuator/hystrix.stream，如http://localhost:8001/actuator/hystrix.stream"></a>输入服务提供者的url+/actuator/hystrix.stream，如<a href="http://localhost:8001/actuator/hystrix.stream" target="_blank" rel="noopener">http://localhost:8001/actuator/hystrix.stream</a></h6></li>
</ul>
<p><img src="http://148.70.50.70/source/1574773579130.jpg" alt="image.png"></p>
<ul>
<li><h6 id="监控成功"><a href="#监控成功" class="headerlink" title="监控成功"></a>监控成功</h6></li>
</ul>
<h4 id="Zuul路由网关"><a href="#Zuul路由网关" class="headerlink" title="Zuul路由网关"></a>Zuul路由网关</h4><p>Zuul包含了对请求的路由和过滤两个最主要的功能：其中路由功能负责将外部请求转发到具体的微服务实例上，是实现外部访问统一入口的基础，而过滤器功能则负责对请求的处理过程进行干预，是实现请求校验，服务聚合等功能的基础。Zuul和Eureka进行整合，将Zuul自身注册为Eureka服务治理下的应用，同时从Eureka中获得其他微服务的消息，也即以后的访问微服务都是通过Zuul跳转后获得。</p>
<p><strong>注意: Zuul服务最终还是会注册进Eureka</strong></p>
<p><strong>提供:代理+路由+过滤三大功能!</strong> </p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#微服务架构之SpringCloud"><span class="toc-number">1.</span> <span class="toc-text">微服务架构之SpringCloud</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#学习前提"><span class="toc-number">1.0.0.1.</span> <span class="toc-text">学习前提</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#微服务架构四个核心问题"><span class="toc-number">1.0.0.2.</span> <span class="toc-text">微服务架构四个核心问题</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#解决方案"><span class="toc-number">1.0.0.2.0.1.</span> <span class="toc-text">解决方案</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#微服务与微服务架构"><span class="toc-number">1.0.0.3.</span> <span class="toc-text">微服务与微服务架构</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#微服务"><span class="toc-number">1.0.0.3.1.</span> <span class="toc-text">微服务</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#微服务架构"><span class="toc-number">1.0.0.3.2.</span> <span class="toc-text">微服务架构</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SpringCloud-Begin"><span class="toc-number">1.0.1.</span> <span class="toc-text">SpringCloud Begin</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#基础项目搭建"><span class="toc-number">1.0.1.1.</span> <span class="toc-text">基础项目搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#父项目"><span class="toc-number">1.0.1.1.1.</span> <span class="toc-text">父项目</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#写一个api提供者"><span class="toc-number">1.0.1.1.2.</span> <span class="toc-text">写一个api提供者</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建一个服务提供者，添加依赖"><span class="toc-number">1.0.1.1.3.</span> <span class="toc-text">创建一个服务提供者，添加依赖</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建消费者consumer"><span class="toc-number">1.0.1.1.4.</span> <span class="toc-text">创建消费者consumer</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#简单配置pom"><span class="toc-number">1.0.1.1.4.1.</span> <span class="toc-text">简单配置pom</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加配置，创建一个REST配置类RestConfig"><span class="toc-number">1.0.1.1.4.2.</span> <span class="toc-text">添加配置，创建一个REST配置类RestConfig</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#创建一个UserConsumerController类"><span class="toc-number">1.0.1.1.4.3.</span> <span class="toc-text">创建一个UserConsumerController类</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#application-yml配置端口为8080，启动provider提供者，然后访问"><span class="toc-number">1.0.1.1.4.4.</span> <span class="toc-text">application.yml配置端口为8080，启动provider提供者，然后访问</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#添加Eureka服务注册中心"><span class="toc-number">1.0.1.2.</span> <span class="toc-text">添加Eureka服务注册中心</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#修改一下host文件模拟不同地址"><span class="toc-number">1.0.1.2.1.</span> <span class="toc-text">修改一下host文件模拟不同地址</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建一个Eureka服务端"><span class="toc-number">1.0.1.2.2.</span> <span class="toc-text">创建一个Eureka服务端</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖"><span class="toc-number">1.0.1.2.2.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置yml"><span class="toc-number">1.0.1.2.2.2.</span> <span class="toc-text">配置yml</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端"><span class="toc-number">1.0.1.2.2.3.</span> <span class="toc-text">再次回到我们的provider中，在主启动类中添加注解，标明Eureka客户端</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#也可以在Controller中写一下获取微服务实例的代码"><span class="toc-number">1.0.1.2.2.4.</span> <span class="toc-text">也可以在Controller中写一下获取微服务实例的代码</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#最重要的就是配置yml，在之前的yml中添加，最重要的就是spring-application-name（这个会是一个服务标识）和eureka-client-service-url（这是注册中心的地址）"><span class="toc-number">1.0.1.2.2.5.</span> <span class="toc-text">最重要的就是配置yml，在之前的yml中添加，最重要的就是spring.application.name（这个会是一个服务标识）和eureka.client.service-url（这是注册中心的地址）</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务"><span class="toc-number">1.0.1.2.2.6.</span> <span class="toc-text">首先启动Eureka服务注册中心，然后启动provider，就可以发现provider成功向注册中心提供了服务</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#配置Eureka集群环境"><span class="toc-number">1.0.1.3.</span> <span class="toc-text">配置Eureka集群环境</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003"><span class="toc-number">1.0.1.3.1.</span> <span class="toc-text">首先再次创建两个Eureka注册中心，分别命名为springcloud-eureka-7002和springcloud-eureka-7003</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-1"><span class="toc-number">1.0.1.3.1.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类配置注解"><span class="toc-number">1.0.1.3.1.2.</span> <span class="toc-text">主启动类配置注解</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置yml-1"><span class="toc-number">1.0.1.3.1.3.</span> <span class="toc-text">配置yml</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7002的配置"><span class="toc-number">1.0.1.3.1.4.</span> <span class="toc-text">7002的配置</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#7003"><span class="toc-number">1.0.1.3.1.5.</span> <span class="toc-text">7003</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#修改provider，把服务提供到的注册中心由一个增加到三个"><span class="toc-number">1.0.1.3.2.</span> <span class="toc-text">修改provider，把服务提供到的注册中心由一个增加到三个</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在yml中"><span class="toc-number">1.0.1.3.2.1.</span> <span class="toc-text">在yml中</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#Eureka熔断机制"><span class="toc-number">1.0.1.3.3.</span> <span class="toc-text">Eureka熔断机制</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#自我保护机制"><span class="toc-number">1.0.1.3.3.1.</span> <span class="toc-text">自我保护机制</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#CAP原则"><span class="toc-number">1.0.1.3.3.2.</span> <span class="toc-text">CAP原则</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Ribben负载均衡"><span class="toc-number">1.0.1.4.</span> <span class="toc-text">Ribben负载均衡</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#修改consumer"><span class="toc-number">1.0.1.4.1.</span> <span class="toc-text">修改consumer</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在上面基础的配置下添加"><span class="toc-number">1.0.1.4.1.1.</span> <span class="toc-text">在上面基础的配置下添加</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#将原来的UserConsumerController中的URL换成"><span class="toc-number">1.0.1.4.1.2.</span> <span class="toc-text">将原来的UserConsumerController中的URL换成</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置注册中心地址（从哪里拿）"><span class="toc-number">1.0.1.4.1.3.</span> <span class="toc-text">配置注册中心地址（从哪里拿）</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在最外层的同级包下创建一个规则配置rule，具体位置"><span class="toc-number">1.0.1.4.1.4.</span> <span class="toc-text">在最外层的同级包下创建一个规则配置rule，具体位置</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#开启负载均衡，修改RestConfig"><span class="toc-number">1.0.1.4.1.5.</span> <span class="toc-text">开启负载均衡，修改RestConfig</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加负载均衡随机轮询规则"><span class="toc-number">1.0.1.4.1.6.</span> <span class="toc-text">添加负载均衡随机轮询规则</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#最后在启动类中添加注解"><span class="toc-number">1.0.1.4.1.7.</span> <span class="toc-text">最后在启动类中添加注解</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#consumer修改好了后，开始修改provider"><span class="toc-number">1.0.1.4.2.</span> <span class="toc-text">consumer修改好了后，开始修改provider</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加两个数据库"><span class="toc-number">1.0.1.4.2.1.</span> <span class="toc-text">添加两个数据库</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#都是同样的内容"><span class="toc-number">1.0.1.4.2.2.</span> <span class="toc-text">都是同样的内容</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加两个服务提供者provider-8002-provider-8003"><span class="toc-number">1.0.1.4.2.3.</span> <span class="toc-text">添加两个服务提供者provider-8002 provider-8003</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-2"><span class="toc-number">1.0.1.4.2.4.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#修改后的三个provider后的application-yml为"><span class="toc-number">1.0.1.4.2.5.</span> <span class="toc-text">修改后的三个provider后的application.yml为</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#测试"><span class="toc-number">1.0.1.4.3.</span> <span class="toc-text">测试</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#启动一个Eureka，三个provider以及一个consumer"><span class="toc-number">1.0.1.4.3.1.</span> <span class="toc-text">启动一个Eureka，三个provider以及一个consumer</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#feign接口方式调用服务"><span class="toc-number">1.0.1.5.</span> <span class="toc-text">feign接口方式调用服务</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#springcloud-api中"><span class="toc-number">1.0.1.5.1.</span> <span class="toc-text">springcloud-api中</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-3"><span class="toc-number">1.0.1.5.1.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#添加service-UserService接口"><span class="toc-number">1.0.1.5.1.2.</span> <span class="toc-text">添加service.UserService接口</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#创建一个feign测试项目springcloud-feign"><span class="toc-number">1.0.1.5.2.</span> <span class="toc-text">创建一个feign测试项目springcloud-feign</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-4"><span class="toc-number">1.0.1.5.2.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置application-yml"><span class="toc-number">1.0.1.5.2.2.</span> <span class="toc-text">配置application.yml</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类，添加扫描包注解"><span class="toc-number">1.0.1.5.2.3.</span> <span class="toc-text">主启动类，添加扫描包注解</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#创建UserConsumerController类"><span class="toc-number">1.0.1.5.2.4.</span> <span class="toc-text">创建UserConsumerController类</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#配置一下RestConfig"><span class="toc-number">1.0.1.5.2.5.</span> <span class="toc-text">配置一下RestConfig</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#其实和上面的consumer是一样的，只不过有些许地方不太一样"><span class="toc-number">1.0.1.5.2.6.</span> <span class="toc-text">其实和上面的consumer是一样的，只不过有些许地方不太一样</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#服务熔断"><span class="toc-number">1.0.1.6.</span> <span class="toc-text">服务熔断</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#分布式系统面临的问题"><span class="toc-number">1.0.1.6.0.1.</span> <span class="toc-text">分布式系统面临的问题</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#服务雪崩"><span class="toc-number">1.0.1.6.0.2.</span> <span class="toc-text">服务雪崩</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Hystrix"><span class="toc-number">1.0.1.6.0.3.</span> <span class="toc-text">Hystrix</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Hystrix用处"><span class="toc-number">1.0.1.6.0.4.</span> <span class="toc-text">Hystrix用处</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#服务熔断-1"><span class="toc-number">1.0.1.6.0.5.</span> <span class="toc-text">服务熔断</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#添加一个子项目Hystrix实现服务熔断处理"><span class="toc-number">1.0.1.6.1.</span> <span class="toc-text">添加一个子项目Hystrix实现服务熔断处理</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-5"><span class="toc-number">1.0.1.6.1.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类添加注解-EnableCircuitBreaker"><span class="toc-number">1.0.1.6.1.2.</span> <span class="toc-text">主启动类添加注解@EnableCircuitBreaker</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在controller中添加应对熔断的方法hystrixGet"><span class="toc-number">1.0.1.6.1.3.</span> <span class="toc-text">在controller中添加应对熔断的方法hystrixGet</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法"><span class="toc-number">1.0.1.6.1.4.</span> <span class="toc-text">同时对需要熔断机制的方法中添加注解，也是在controller中添加一个回调方法</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#服务降级"><span class="toc-number">1.0.1.7.</span> <span class="toc-text">服务降级</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#在springcloud-api中"><span class="toc-number">1.0.1.7.1.</span> <span class="toc-text">在springcloud-api中</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在service包下，创建一个UserServiceFallback类，记得要加-Component注解"><span class="toc-number">1.0.1.7.1.1.</span> <span class="toc-text">在service包下，创建一个UserServiceFallback类，记得要加@Component注解</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在service中，修改UserService代码，添加回调类"><span class="toc-number">1.0.1.7.1.2.</span> <span class="toc-text">在service中，修改UserService代码，添加回调类</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在客户端，yml添加配置开启降级"><span class="toc-number">1.0.1.7.1.3.</span> <span class="toc-text">在客户端，yml添加配置开启降级</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#然后在启动类中扫描包，-SpringBootApplication一定要扫描api下的包和本地的包，不然会报错"><span class="toc-number">1.0.1.7.1.4.</span> <span class="toc-text">然后在启动类中扫描包，@SpringBootApplication一定要扫描api下的包和本地的包，不然会报错</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#测试-1"><span class="toc-number">1.0.1.7.1.5.</span> <span class="toc-text">测试</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。"><span class="toc-number">1.0.1.7.1.6.</span> <span class="toc-text">可见，熔断机制是在服务端也就是provider中写的，而服务降级是在客户端也就是consumer中。</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Hystrix-Dashboard流监控"><span class="toc-number">1.0.1.8.</span> <span class="toc-text">Hystrix Dashboard流监控</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#新建一个监控面板springcloud-hystrix-dashboard"><span class="toc-number">1.0.1.8.1.</span> <span class="toc-text">新建一个监控面板springcloud-hystrix-dashboard</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#设定为9001端口"><span class="toc-number">1.0.1.8.1.1.</span> <span class="toc-text">设定为9001端口</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#主启动类添加注解-EnableHystrixDashboard"><span class="toc-number">1.0.1.8.1.2.</span> <span class="toc-text">主启动类添加注解@EnableHystrixDashboard</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#在服务提供者中"><span class="toc-number">1.0.1.8.2.</span> <span class="toc-text">在服务提供者中</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#添加依赖-6"><span class="toc-number">1.0.1.8.2.1.</span> <span class="toc-text">添加依赖</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#在启动类中添加Bean"><span class="toc-number">1.0.1.8.2.2.</span> <span class="toc-text">在启动类中添加Bean</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#测试-2"><span class="toc-number">1.0.1.8.3.</span> <span class="toc-text">测试</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#启动Eureka注册中心"><span class="toc-number">1.0.1.8.3.1.</span> <span class="toc-text">启动Eureka注册中心</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#启动服务提供者"><span class="toc-number">1.0.1.8.3.2.</span> <span class="toc-text">启动服务提供者</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#启动Hystrix监控面板"><span class="toc-number">1.0.1.8.3.3.</span> <span class="toc-text">启动Hystrix监控面板</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#打开http-localhost-9001-hystrix"><span class="toc-number">1.0.1.8.3.4.</span> <span class="toc-text">打开http://localhost:9001/hystrix/</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#输入服务提供者的url-actuator-hystrix-stream，如http-localhost-8001-actuator-hystrix-stream"><span class="toc-number">1.0.1.8.3.5.</span> <span class="toc-text">输入服务提供者的url+/actuator/hystrix.stream，如http://localhost:8001/actuator/hystrix.stream</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#监控成功"><span class="toc-number">1.0.1.8.3.6.</span> <span class="toc-text">监控成功</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Zuul路由网关"><span class="toc-number">1.0.1.9.</span> <span class="toc-text">Zuul路由网关</span></a></li></ol></li></ol></li></ol></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&text=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&is_video=false&description=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=微服务架构之SpringCloud&body=Check out this article: http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/" target="_blank" rel="noopener"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&title=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&name=微服务架构之SpringCloud&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/11/26/%E5%BE%AE%E6%9C%8D%E5%8A%A1%E6%9E%B6%E6%9E%84%E4%B9%8BSpringCloud/&t=微服务架构之SpringCloud" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 ECIN520
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">

    <!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<!-- clipboard -->

  <script src="/lib/clipboard/clipboard.min.js"></script>
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
